package com.ming.mallchat.common.user.service.adpater;

import com.ming.mallchat.common.common.domain.enums.YesOrNoEnum;
import com.ming.mallchat.common.user.domain.vo.resp.user.BadgeResp;
import com.ming.mallchat.common.user.pojo.ItemConfig;
import com.ming.mallchat.common.user.pojo.User;
import com.ming.mallchat.common.user.domain.vo.resp.user.UserInfoResp;
import com.ming.mallchat.common.user.pojo.UserBackpack;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import org.springframework.beans.BeanUtils;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author 86150
 * @date 2023/09/26
 */
public class UserAdapter {
    public static User buildUserSave(String openId) {
        return User.builder().openId(openId).build();
    }

    public static User buildAuthorUserSave(User user, WxOAuth2UserInfo userInfo) {
        user.setName(userInfo.getNickname());
        user.setSex(userInfo.getSex());
        user.setAvatar(userInfo.getHeadImgUrl());
        return user;
    }

    public static UserInfoResp buildUserInfo(User user, Integer count) {
        UserInfoResp vo = new UserInfoResp();
        BeanUtils.copyProperties(user, vo);
        vo.setModifyNameChance(count);
        return vo;
    }

    public static List<BadgeResp> buildBadge(List<ItemConfig> itemConfig, List<UserBackpack> userBackpacks, User user) {
        Set<Long> obtainItemSet = userBackpacks.stream().map(UserBackpack::getItemId).collect(Collectors.toSet());
        return itemConfig.stream().map(a -> {
            BadgeResp resp = new BadgeResp();
            BeanUtils.copyProperties(a, resp);
            resp.setObtain(obtainItemSet.contains(a.getId()) ? YesOrNoEnum.YES.getStatus() : YesOrNoEnum.NO.getStatus());
            resp.setWearing(Objects.equals(a.getId(), user.getItemId()) ? YesOrNoEnum.YES.getStatus() : YesOrNoEnum.NO.getStatus());
            return resp;
        }).sorted(Comparator.comparing(BadgeResp::getWearing,Comparator.reverseOrder())
                .thenComparing(BadgeResp::getObtain,Comparator.reverseOrder()))
                .collect(Collectors.toList());
    }
}
